package utils

import (
	"github.com/jinzhu/gorm"
	"model/game"
	//"fmt"
	"reflect"
	"time"
	"strconv"
	"math"
	"model/user"
	"encoding/json"
	"crypto/md5"
	"encoding/hex"
	//"fmt"
	"fmt"
	"github.com/garyburd/redigo/redis"
)
var Layout = "2006-01-02 15:04:05"
var MemcacheServer = "127.0.0.1:11211"
//interface转换map
func IToMap(Is interface{}) map[string]interface{} {
	t:=reflect.TypeOf(Is)
	var v reflect.Value
	if t.Kind() == reflect.Ptr{
		v = reflect.ValueOf(Is).Elem()
	}else{
		v = reflect.ValueOf(Is)
	}
	if v.Kind() != reflect.Slice {
		panic("toslice Is not slice")
	}
	l := v.Len()
	d := make(map[string]interface{}, l)
	//ret := make([]interface{}, l)
	for i := 0; i < l; i++ {
		item := v.Index(i).Interface()
		s := strconv.Itoa(i)
		d[s] = Struct2Map(item)
	}
	return  d
}
func IToMap2(Is interface{}) map[string]interface{} {
	t:=reflect.TypeOf(Is)
	var v reflect.Value
	if t.Kind() == reflect.Ptr{
		v = reflect.ValueOf(Is).Elem()
	}else{
		v = reflect.ValueOf(Is)
	}
	//if v.Kind() != reflect.Slice {
	//	panic("toslice Is not slice")
	//}
	l := v.Len()
	d := make(map[string]interface{}, l)
	//ret := make([]interface{}, l)
	for i := 0; i < l; i++ {
		item := v.Index(i).Interface()
		s := strconv.Itoa(i)
		d[s] = item
	}
	return  d
}


//获取分页列表
func GetPageList(Db *gorm.DB,results interface{} ,shops interface{},selectStr string,where interface{},sort string,prepage int8,page int8,tableName string,isCount bool) (data map[string]interface{}){
	Db.Select(selectStr).Where(where).Order(sort).Offset(page*prepage).Limit(prepage).Find(shops).Scan(results)
	d:=IToMap(results)
	ds := make(map[string]interface{}, prepage)
	ds["list"] = d
	if isCount == true{
		var count int8
		Db.Table(tableName).Select(selectStr).Count(&count);
		ds["rcount"] = count
		ds["pcount"] = math.Ceil(float64(count)/float64(prepage))
	}
	return ds
}
func getMapAsName(resName string,results interface{},d map[string]interface{})  {

	switch resName {
	case "ShopListResult":
		res  := results.(*[]game.ShopListResult)
		for k,v:= range *res{
			s := strconv.Itoa(k)
			d[s] = Struct2Map(v)
		}
	case "UserDepotList":
		res  := results.(*[]user.UserDepotResult)
		for k,v:= range *res{
			s := strconv.Itoa(k)
			d[s] = Struct2Map(v)
		}
	}
}
//结构体转map
func Struct2Map(obj interface{}) map[string]interface{} {
	t := reflect.TypeOf(obj)
	v := reflect.ValueOf(obj)
	var data = make(map[string]interface{})
	for i := 0; i < t.NumField(); i++ {
		vr := v.Field(i).Interface()
		if value, ok := vr.(struct{}); ok {
			data[t.Field(i).Name] = Struct2Map(value)
		} else {
			if t.Field(i).Type.Name()=="Time"{
				timeStr := vr.(time.Time)
				vr = timeStr.Format(Layout)
			}
			//if strings.Contains(selectStr,t.Field(i).Name)==true{
			data[t.Field(i).Name] = vr
			//}
		}
	}
	return data
}
func Md5(s string) string {
	h2 := md5.New()
	h2.Write([]byte(s)) // 需要加密的字符串为 123456
	cipherStr2 := h2.Sum(nil)
	chartListKey:=hex.EncodeToString(cipherStr2)
	return chartListKey
}
func NextTime() int64{
	timeStr := time.Now().Format("2006-01-02")
	t, _ := time.ParseInLocation("2006-01-02 15:04:05", timeStr+" 23:59:59", time.Local)
	tt:=t.Unix() + 1
	return tt
}
func PutTime(timeUnix int64) (b string){
	curTimeUnix:=time.Now().Unix()
	timeLine:=curTimeUnix - timeUnix//时间差值
	if timeLine<=120{
		return "刚刚登陆"
	}
	i:=math.Floor(float64(timeLine/60)) //分钟
	h:=math.Floor(float64(timeLine/60/60)) //小时
	d:=math.Floor(float64(timeLine/86400)) //天
	if i < 30{
		s:=strconv.FormatInt(int64(i), 10)
		return s+"分钟前登陆"
	}
	if i > 30 && i < 60{
		return "一小时内登陆"
	}
	if h>=1 && h < 24{
		s:=strconv.FormatInt(int64(h), 10)
		return s+"小时前登陆"
	}
	if d>=1 && d < 3{
		s:=strconv.FormatInt(int64(d), 10)
		return s+"天前登陆"
	}
	if d>=3 {
		//s:=strconv.FormatInt(int64(d), 10)
		return "3天前登陆"
	}

	return ""
}
func PutTime2(t time.Time) (b string){
	timeUnix:=t.Unix()
	curTimeUnix:=time.Now().Unix()
	timeLine:=curTimeUnix - timeUnix//时间差值
	if timeLine<=120{
		return "刚刚登陆"
	}
	i:=math.Floor(float64(timeLine/60)) //分钟
	h:=math.Floor(float64(timeLine/60/60)) //小时
	d:=math.Floor(float64(timeLine/86400)) //天
	if i < 30{
		s:=strconv.FormatInt(int64(i), 10)
		return s+"分钟前登陆"
	}
	if i > 30 && i < 60{
		return "一小时内登陆"
	}
	if h>=1 && h < 24{
		s:=strconv.FormatInt(int64(h), 10)
		return s+"小时前登陆"
	}
	if d>=1 && d < 3{
		s:=strconv.FormatInt(int64(d), 10)
		return s+"天前登陆"
	}
	if d>=3 {
		//s:=strconv.FormatInt(int64(d), 10)
		return "3天前登陆"
	}

	return ""
}
func GetRedisData(key float64) *map[string]interface{}{
	c, errer := redis.Dial("tcp", "127.0.0.1:6379")
	if errer != nil {
		fmt.Println("Connect to redis error", errer)
	}
	defer c.Close()
	var imapGet map[string]interface{}

	valueGet, errer := redis.Bytes(c.Do("GET", key))
	if errer != nil {
		fmt.Println(errer)
	}
	json.Unmarshal(valueGet, &imapGet)
	fmt.Println(imapGet)
	return &imapGet
}
func SetRedisData(key float64,imap map[string]interface{}) bool{
	c, errer := redis.Dial("tcp", "127.0.0.1:6379")
	if errer != nil {
		fmt.Println("Connect to redis error", errer)
	}
	defer c.Close()

	value, _ := json.Marshal(imap)

	n, err := c.Do("SET", key, value)
	if err != nil {
		fmt.Println(err)
	}
	if n == int64(1) {
		fmt.Println("success")
		return true
	}else{
		return false
	}

}



